home *** CD-ROM | disk | FTP | other *** search
- #include <stdio.h>
- #include <string.h>
- #include <stdlib.h>
- #include <math.h>
- #include "tk.h"
- #include "aux.h"
-
- #define OPENGL_WIDTH 24
- #define OPENGL_HEIGHT 13
-
- GLubyte fly[] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x03, 0x80, 0x01, 0xC0, 0x06, 0xC0, 0x03, 0x60, 0x04, 0x60, 0x06, 0x20,
- 0x04, 0x30, 0x0C, 0x20, 0x04, 0x18, 0x18, 0x20, 0x04, 0x0C, 0x30, 0x20,
- 0x04, 0x06, 0x60, 0x20, 0x44, 0x03, 0xC0, 0x22, 0x44, 0x01, 0x80, 0x22,
- 0x44, 0x01, 0x80, 0x22, 0x44, 0x01, 0x80, 0x22, 0x44, 0x01, 0x80, 0x22,
- 0x44, 0x01, 0x80, 0x22, 0x44, 0x01, 0x80, 0x22, 0x66, 0x01, 0x80, 0x66,
- 0x33, 0x01, 0x80, 0xCC, 0x19, 0x81, 0x81, 0x98, 0x0C, 0xC1, 0x83, 0x30,
- 0x07, 0xe1, 0x87, 0xe0, 0x03, 0x3f, 0xfc, 0xc0, 0x03, 0x31, 0x8c, 0xc0,
- 0x03, 0x33, 0xcc, 0xc0, 0x06, 0x64, 0x26, 0x60, 0x0c, 0xcc, 0x33, 0x30,
- 0x18, 0xcc, 0x33, 0x18, 0x10, 0xc4, 0x23, 0x08, 0x10, 0x63, 0xC6, 0x08,
- 0x10, 0x30, 0x0c, 0x08, 0x10, 0x18, 0x18, 0x08, 0x10, 0x00, 0x00, 0x08};
-
- GLenum rgb, doubleBuffer, directRender, windType;
- GLint objectIndex = 0;
- GLboolean dither = GL_FALSE;
- GLint colormask = 1;
- GLboolean stipple = GL_FALSE;
- GLboolean smooth = GL_FALSE;
- GLboolean clip = GL_FALSE;
- GLboolean zbuffer = GL_FALSE;
- GLboolean perspective = GL_FALSE;
- GLboolean light = GL_FALSE;
- GLboolean cullface = GL_TRUE;
- GLboolean scissor = GL_FALSE;
-
- GLuint bitmapBase = 0;
-
- GLuint bases[20];
- GLint width, height;
-
- GLint angleX_on = 0, angleY_on = 0, angleZ_on = 0;
- GLint shiftX_on = 0, shiftY_on = 0, shiftZ_on = 0;
-
- GLfloat angleX = 0.0, angleY = 0.0, angleZ = 0.0;
- GLfloat scaleX = 0.25, scaleY = 0.25, scaleZ = 0.25;
- GLfloat shiftX = 0.0, shiftY = 0.0, shiftZ = -5.0;
-
- static void Draw(void);
-
- static void CreateAppleFont(void)
- {
- bitmapBase = glGenLists(256);
- aglUseFont(aglGetCurrentContext(), kFontIDNewYork, normal, 10, 0, 256, bitmapBase);
- }
-
- static void DrawStr(char *str)
- {
- glListBase(bitmapBase);
- glCallLists(strlen(str), GL_UNSIGNED_BYTE, (GLubyte *) str);
- }
-
- static void Init(void)
- {
- GLdouble eqn1[4] = {0.0, 1.0, 0.0, 0.0};
- GLfloat light_ambient[] = { 0.5, 0.5, 0.5, 1.0 };
- GLfloat light_diffuse[] = { 0.2, 0.2, 0.4, 1.0 };
- GLfloat light_specular[] = { 0.2, 0.2, 0.4, 1.0 };
- /* light_position is NOT default value */
- GLfloat light_position[] = { 1.0, 1.0, 1.0, 0.0 };
-
- glLightfv (GL_LIGHT0, GL_AMBIENT, light_ambient);
- glLightfv (GL_LIGHT0, GL_DIFFUSE, light_diffuse);
- glLightfv (GL_LIGHT0, GL_SPECULAR, light_specular);
- glLightfv (GL_LIGHT0, GL_POSITION, light_position);
- glLightf (GL_LIGHT0, GL_SPOT_CUTOFF, 180.0);
-
- glEnable(GL_LIGHT0);
- if(light)
- glEnable(GL_LIGHTING);
- else
- glDisable(GL_LIGHTING);
-
- glDepthFunc(GL_LESS);
-
- glPolygonStipple(fly);
- glLineStipple(1, 0xAA);
-
- if(stipple)
- {
- glEnable(GL_LINE_STIPPLE);
- glEnable(GL_POLYGON_STIPPLE);
- }
- else
- {
- glDisable(GL_LINE_STIPPLE);
- glDisable(GL_POLYGON_STIPPLE);
- }
-
- if(zbuffer)
- glEnable(GL_DEPTH_TEST);
- else
- glDisable(GL_DEPTH_TEST);
-
- if(cullface)
- glEnable(GL_CULL_FACE);
- else
- glDisable(GL_CULL_FACE);
-
- if(smooth)
- glShadeModel(GL_SMOOTH);
- else
- glShadeModel(GL_FLAT);
-
- if(dither)
- glEnable(GL_DITHER);
- else
- glDisable(GL_DITHER);
-
- glClipPlane(GL_CLIP_PLANE0, eqn1);
-
- if(clip)
- glEnable(GL_CLIP_PLANE0);
- else
- glDisable(GL_CLIP_PLANE0);
-
- switch(colormask)
- {
- case 1:
- glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
- break;
- case 2:
- glColorMask(GL_FALSE, GL_TRUE, GL_TRUE, GL_TRUE);
- break;
- case 3:
- glColorMask(GL_TRUE, GL_FALSE, GL_TRUE, GL_TRUE);
- break;
- case 4:
- glColorMask(GL_TRUE, GL_TRUE, GL_FALSE, GL_TRUE);
- break;
- case 5:
- glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_FALSE);
- break;
- }
-
- bases[0] = glGenLists(1);
- tkWireSphere(bases[0], 5.0);
-
- bases[1] = glGenLists(1);
- tkSolidSphere(bases[1], 5.0);
-
- bases[2] = glGenLists(1);
- tkWireCube(bases[2], 5.0);
-
- bases[3] = glGenLists(1);
- tkSolidCube(bases[3], 5.0);
-
- bases[4] = glGenLists(1);
- tkWireBox(bases[4], 4.0, 5.0, 6.0);
-
- bases[5] = glGenLists(1);
- tkSolidBox(bases[5], 4.0, 5.0, 6.0);
-
- bases[6] = glGenLists(1);
- tkWireTorus(bases[6], 2.5, 5.0);
-
- bases[7] = glGenLists(1);
- tkSolidTorus(bases[7], 2.5, 5.0);
-
- bases[8] = glGenLists(1);
- tkWireCylinder(bases[8], 5.0, 5.0);
-
- bases[9] = glGenLists(1);
- tkSolidCylinder(bases[9], 5.0, 5.0);
-
- bases[10] = glGenLists(1);
- tkWireIcosahedron(bases[10], 5.0);
-
- bases[11] = glGenLists(1);
- tkSolidIcosahedron(bases[11], 5.0);
-
- bases[12] = glGenLists(1);
- tkWireOctahedron(bases[12], 5.0);
-
- bases[13] = glGenLists(1);
- tkSolidOctahedron(bases[13], 5.0);
-
- bases[14] = glGenLists(1);
- tkWireTetrahedron(bases[14], 5.0);
-
- bases[15] = glGenLists(1);
- tkSolidTetrahedron(bases[15], 5.0);
-
- bases[16] = glGenLists(1);
- tkWireDodecahedron(bases[16], 5.0);
-
- bases[17] = glGenLists(1);
- tkSolidDodecahedron(bases[17], 5.0);
-
- bases[18] = glGenLists(1);
- tkWireCone(bases[18], 5.0, 5.0);
-
- bases[19] = glGenLists(1);
- tkSolidCone(bases[19], 5.0, 5.0);
-
- glClearColor(0.0, 0.0, 0.0, 0.0);
- glClearIndex(0.0);
-
- CreateAppleFont();
-
- glColor3f(1.0, 0.0, 0.0);
- }
-
- static void Reshape(int w, int h)
- {
- glViewport (0, 0, w, h);
-
- width = w;
- height = h;
-
- glMatrixMode (GL_PROJECTION);
- glLoadIdentity ();
-
- if(perspective)
- {
- gluPerspective (60.0, (GLfloat)height/(GLfloat)width, 1.0, 20.0);
- }
- else
- {
- if (width <= height)
- glOrtho (-2.5, 2.5, -2.5*(GLfloat)height/(GLfloat)width,
- 2.5*(GLfloat)height/(GLfloat)width, -10.0, 10.0);
- else
- glOrtho (-2.5*(GLfloat)width/(GLfloat)height,
- 2.5*(GLfloat)width/(GLfloat)height, -2.5, 2.5, -10.0, 10.0);
- }
-
- glMatrixMode (GL_MODELVIEW);
- }
-
- static void SetWireOptions(void)
- {
- angleX_on = 0;
- angleY_on = 0;
- angleZ_on = 0;
-
- shiftX_on = 0;
- shiftY_on = 0;
- shiftZ_on = 0;
-
- scaleX = 0.25;
- scaleY = 0.25;
- scaleZ = 0.25;
-
- shiftX = 0.0;
- shiftY = 0.0;
- shiftZ = -5.0;
-
- glDisable(GL_DITHER);
- dither = GL_FALSE;
-
- colormask = 1;
- glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
-
- glDisable(GL_LINE_STIPPLE);
- glDisable(GL_POLYGON_STIPPLE);
- stipple = GL_FALSE;
-
- glShadeModel(GL_FLAT);
- smooth = GL_FALSE;
-
- light = GL_FALSE;
- glDisable(GL_LIGHTING);
-
- glDisable(GL_DEPTH_TEST);
- zbuffer = GL_FALSE;
-
- glEnable(GL_CULL_FACE);
- cullface = GL_TRUE;
-
- glDisable(GL_CLIP_PLANE0);
- clip = GL_FALSE;
- }
-
- static void SetSolidOptions(void)
- {
- angleX_on = 0;
- angleY_on = 0;
- angleZ_on = 0;
-
- shiftX_on = 0;
- shiftY_on = 0;
- shiftZ_on = 0;
-
- scaleX = 0.25;
- scaleY = 0.25;
- scaleZ = 0.25;
-
- shiftX = 0.0;
- shiftY = 0.0;
- shiftZ = -5.0;
-
- glDisable(GL_DITHER);
- dither = GL_FALSE;
-
- colormask = 1;
- glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
-
- glDisable(GL_LINE_STIPPLE);
- glDisable(GL_POLYGON_STIPPLE);
- stipple = GL_FALSE;
-
- glShadeModel(GL_FLAT);
- smooth = GL_FALSE;
-
- light = GL_TRUE;
- glEnable(GL_LIGHTING);
-
- glDisable(GL_DEPTH_TEST);
- zbuffer = GL_FALSE;
-
- glEnable(GL_CULL_FACE);
- cullface = GL_TRUE;
-
- glDisable(GL_CLIP_PLANE0);
- clip = GL_FALSE;
- }
-
- static GLenum KeyDown(int key, GLenum mask)
- {
- switch (key)
- {
- case TK_ESCAPE:
- tkQuit();
- break;
- case TK_SPACE:
- objectIndex++;
- if(objectIndex > 19) objectIndex = 0;
-
- if(objectIndex % 2 == 0) SetWireOptions();
- else SetSolidOptions();
- break;
- case TK_LEFT:
- shiftX_on = -1;
- break;
- case TK_RIGHT:
- shiftX_on = 1;
- break;
- case TK_UP:
- shiftY_on = 1;
- break;
- case TK_DOWN:
- shiftY_on = -1;
- break;
- case TK_n:
- shiftZ_on = 1;
- break;
- case TK_m:
- shiftZ_on = -1;
- break;
- case TK_q:
- scaleX -= 0.1;
- if (scaleX < 0.1) {
- scaleX = 0.1;
- }
- break;
- case TK_w:
- scaleX += 0.1;
- break;
- case TK_a:
- scaleY -= 0.1;
- if (scaleY < 0.1) {
- scaleY = 0.1;
- }
- break;
- case TK_s:
- scaleY += 0.1;
- break;
- case TK_z:
- scaleZ -= 0.1;
- if (scaleZ < 0.1) {
- scaleZ = 0.1;
- }
- break;
- case TK_x:
- scaleZ += 0.1;
- break;
- case TK_e:
- angleX_on = -1;
- break;
- case TK_r:
- angleX_on = 1;
- break;
- case TK_d:
- angleY_on = -1;
- break;
- case TK_f:
- angleY_on = 1;
- break;
- case TK_c:
- angleZ_on = -1;
- break;
- case TK_v:
- angleZ_on = 1;
- break;
- case TK_y:
- if(dither)
- {
- glDisable(GL_DITHER);
- dither = GL_FALSE;
- }
- else
- {
- glEnable(GL_DITHER);
- dither = GL_TRUE;
- }
- break;
- case TK_j:
- colormask++;
- if(colormask > 5) colormask = 1;
- switch(colormask)
- {
- case 1:
- glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
- break;
- case 2:
- glColorMask(GL_FALSE, GL_TRUE, GL_TRUE, GL_TRUE);
- break;
- case 3:
- glColorMask(GL_TRUE, GL_FALSE, GL_TRUE, GL_TRUE);
- break;
- case 4:
- glColorMask(GL_TRUE, GL_TRUE, GL_FALSE, GL_TRUE);
- break;
- case 5:
- glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_FALSE);
- break;
- }
- break;
- case TK_k:
- if(stipple)
- {
- glDisable(GL_LINE_STIPPLE);
- glDisable(GL_POLYGON_STIPPLE);
- stipple = GL_FALSE;
- }
- else
- {
- glEnable(GL_LINE_STIPPLE);
- glEnable(GL_POLYGON_STIPPLE);
- stipple = GL_TRUE;
- }
- break;
- case TK_l:
- if(smooth)
- {
- glShadeModel(GL_FLAT);
- smooth = GL_FALSE;
- }
- else
- {
- glShadeModel(GL_SMOOTH);
- smooth = GL_TRUE;
- }
- break;
- case TK_h:
- if(clip)
- {
- glDisable(GL_CLIP_PLANE0);
- clip = GL_FALSE;
- }
- else
- {
- glEnable(GL_CLIP_PLANE0);
- clip = GL_TRUE;
- }
- break;
- case TK_p:
- if(perspective)
- perspective = GL_FALSE;
- else
- perspective = GL_TRUE;
-
- glMatrixMode (GL_PROJECTION);
- glLoadIdentity ();
-
- if(perspective)
- {
- gluPerspective (60.0, (GLfloat)height/(GLfloat)width, 1.0, 20.0);
- }
- else
- {
- if (width <= height)
- glOrtho (-2.5, 2.5, -2.5*(GLfloat)height/(GLfloat)width,
- 2.5*(GLfloat)height/(GLfloat)width, -20.0, 20.0);
- else
- glOrtho (-2.5*(GLfloat)width/(GLfloat)height,
- 2.5*(GLfloat)width/(GLfloat)height, -2.5, 2.5, -20.0, 20.0);
- }
-
- glMatrixMode (GL_MODELVIEW);
- break;
- case TK_o:
- if(light)
- {
- light = GL_FALSE;
- glDisable(GL_LIGHTING);
- }
- else
- {
- light = GL_TRUE;
- glEnable(GL_LIGHTING);
- }
- break;
- case TK_u:
- if(zbuffer)
- {
- glDisable(GL_DEPTH_TEST);
- zbuffer = GL_FALSE;
- }
- else
- {
- glEnable(GL_DEPTH_TEST);
- zbuffer = GL_TRUE;
- }
- break;
- case TK_i:
- if(cullface)
- {
- glDisable(GL_CULL_FACE);
- cullface = GL_FALSE;
- }
- else
- {
- glEnable(GL_CULL_FACE);
- cullface = GL_TRUE;
- }
- break;
- case TK_b:
- if(scissor)
- {
- glDisable(GL_SCISSOR_TEST);
- }
- else
- {
- glScissor(50, 50, 300, 300);
- glEnable(GL_SCISSOR_TEST);
- }
-
- scissor = !scissor;
- break;
- default:
- return GL_FALSE;
- }
-
- return GL_TRUE;
- }
-
- static double filter(double in, double *save)
- {
- static double k1 = 0.9;
- static double k2 = 0.05;
-
- save[3] = in;
- save[1] = save[0]*k1 + k2*(save[3] + save[2]);
-
- save[0]=save[1];
- save[2]=save[3];
-
- return(save[1]);
- }
-
- static void Draw(void)
- {
- static double th[4] = {0.0, 0.0, 0.0, 0.0};
- static double t1 = 0.0, t2 = 0.0, t;
- char num_str[200];
-
- t1 = t2;
-
- if(shiftX_on < 0) shiftX -= 0.075;
- else if(shiftX_on > 0) shiftX += 0.075;
-
- if(shiftY_on < 0) shiftY -= 0.075;
- else if(shiftY_on > 0) shiftY += 0.075;
-
- if(shiftZ_on < 0) shiftZ -= 0.075;
- else if(shiftZ_on > 0) shiftZ += 0.075;
-
- if(angleX_on < 0)
- {
- angleX -= 5.0;
- if(angleX < -180.0) angleX += 360;
- }
- else if(angleX_on > 0)
- {
- angleX += 5.0;
- if(angleX > 180.0) angleX -= 360;
- }
-
- if(angleY_on < 0)
- {
- angleY -= 5.0;
- if(angleY < -180.0) angleY += 360;
- }
- else if(angleY_on > 0)
- {
- angleY += 5.0;
- if(angleY > 180.0) angleY -= 360;
- }
-
- if(angleZ_on < 0)
- {
- angleZ -= 5.0;
- if(angleZ < -180.0) angleZ += 360;
- }
- else if(angleZ_on > 0)
- {
- angleZ += 5.0;
- if(angleZ > 180.0) angleZ -= 360;
- }
-
- shiftX_on = 0;
- shiftY_on = 0;
- shiftZ_on = 0;
- angleX_on = 0;
- angleY_on = 0;
- angleZ_on = 0;
-
- glClear(GL_COLOR_BUFFER_BIT);
- if(zbuffer) glClear(GL_DEPTH_BUFFER_BIT);
-
- glMatrixMode(GL_MODELVIEW);
- glPushMatrix();
-
- glTranslatef(shiftX, shiftY, shiftZ);
- glRotatef(angleX, 1.0, 0.0, 0.0);
- glRotatef(angleY, 0.0, 1.0, 0.0);
- glRotatef(angleZ, 0.0, 0.0, 1.0);
- glScalef(scaleX, scaleY, scaleZ);
-
- glCallList(bases[objectIndex]);
-
- glPopMatrix();
-
- t2 = tkNow();
- t = t2 - t1;
- if(t > 0.0001) t = 1.0 / t;
-
- if(light) glDisable(GL_LIGHTING);
- if(zbuffer) glDisable(GL_DEPTH_TEST);
-
- glColor3f(1.0, 0.0, 0.0);
-
- glMatrixMode (GL_PROJECTION);
- glPushMatrix();
- glLoadIdentity();
- glOrtho(-10.0, 10.0, -10.0, 10.0, -10.0, 10.0);
-
- glRasterPos2f(-9.9, 9.4);
- sprintf(num_str, "%0.2f Hz", filter(t, th));
- DrawStr(num_str);
-
- glRasterPos2f(-9.9, -9.9);
- sprintf(num_str, "Hit space bar for next object (read key cmds for other options)");
- DrawStr(num_str);
-
- glPopMatrix();
- glMatrixMode(GL_MODELVIEW);
-
- if(light) glEnable(GL_LIGHTING);
- if(zbuffer) glEnable(GL_DEPTH_TEST);
-
- tkSwapBuffers();
- }
-
- static GLenum Args(int argc, char **argv)
- {
- GLint i;
-
- rgb = GL_TRUE;
- doubleBuffer = GL_TRUE;
- directRender = GL_TRUE;
-
- for (i = 1; i < argc; i++) {
- if (strcmp(argv[i], "-ci") == 0) {
- rgb = GL_FALSE;
- } else if (strcmp(argv[i], "-rgb") == 0) {
- rgb = GL_TRUE;
- } else if (strcmp(argv[i], "-sb") == 0) {
- doubleBuffer = GL_FALSE;
- } else if (strcmp(argv[i], "-db") == 0) {
- doubleBuffer = GL_TRUE;
- } else if (strcmp(argv[i], "-dr") == 0) {
- directRender = GL_TRUE;
- } else if (strcmp(argv[i], "-ir") == 0) {
- directRender = GL_FALSE;
- } else {
- printf("%s (Bad option).\n", argv[i]);
- return GL_FALSE;
- }
- }
- return GL_TRUE;
- }
-
- void main(int argc, char **argv)
- {
- if(!Args(argc, argv)) tkQuit();
-
- tkInitPosition(30, 60, 400, 400);
-
- windType = (rgb) ? TK_RGB : TK_INDEX;
- windType |= TK_DEPTH;
- windType |= (doubleBuffer) ? TK_DOUBLE : TK_SINGLE;
- windType |= (directRender) ? TK_DIRECT : TK_INDIRECT;
- tkInitDisplayMode(windType);
-
- if(!tkInitWindow("Shape")) tkQuit();
-
- Init();
-
- tkExposeFunc(Reshape);
- tkReshapeFunc(Reshape);
- tkKeyDownFunc(KeyDown);
- tkDisplayFunc(Draw);
- tkExec();
- }
-